﻿Imports System.ComponentModel

Public Class frmFinancialSetupGlobalPrint

    Private mFrmLedgerList As List(Of ClsLedger) '保存系统中所有账套信息
    Private mIfSelectedIndustryAcct As Boolean '是否已经选择了行业科目
    Private mIfSettingEdit As Boolean '控件内容是否已修改过
    Private mIfSavedEdit As Boolean '当前修改内容是否已存盘

    '订阅对象
    Private m_Broker As ClsBroker = New ClsBroker()

    ''' <summary>
    ''' 订阅事件的回调函数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e">传递的参数,包含行业科目类对象</param>
    Private Sub GetSelectedIndustryAcctType(ByVal sender As Object, ByVal e As TransEventArg)
        '将收到的行业科目类对象加载到控件对象上
        Dim tmpAcctType As ClsIndustryAccountType
        If "IndustryAcctType".Equals(e.Msg) Then
            tmpAcctType = CType(e.Obj, ClsIndustryAccountType)
        Else
            Return
        End If

        '修改控件内容
        With tmpAcctType
            Me.txtIndustryType.Text = .Industry
            Me.txtOneAcctLength.Text = .OneLevelAcctLength.ToString
            Me.txtSubAcctLength.Text = .NewAcctLength.ToString
            Me.txtCashCode.Text = .CashCode
            Me.txtBankCode.Text = .BankCode
            Me.txtAssets.Text = .AssetsCode
            Me.txtDepCode.Text = .DepCode
            Me.txtReceiveCode.Text = .RecieveCode
            Me.txtPayCode.Text = .PayCode
            Me.txtGainCode.Text = .GainCode
            Me.txtDevCode.Text = .DevCode
            Me.txtStockCode.Text = .StockCode
            Me.txtIncAndDecCode.Text = .IncAndDecCode
        End With

        '标记选择了行业科目
        mIfSelectedIndustryAcct = True
    End Sub

    Private Sub OnCustomControlTextChanged(ByVal sender As Object, ByVal e As CustomControlChangeNotifyEventArg)
        '此时控件内容被改变
        mIfSettingEdit = True

        'Debug.Print("文本控件内容被改变")
    End Sub
    Private Sub frmFinancialSetupGlobalPrint_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '订阅 行业科目选择 事件
        AddHandler ClsBroker.OnChange, AddressOf GetSelectedIndustryAcctType

        '测试自定义通知控件
        AddHandler ClsBroker.OnCustomCtrolContentChange, AddressOf OnCustomControlTextChanged


        '锁定不可以手工修改的控件
        Me.txtIndustryType.ReadOnly = True
        Me.txtOneAcctLength.ReadOnly = True

        Me.txtCashCode.ReadOnly = True
        Me.txtBankCode.ReadOnly = True
        Me.txtAssets.ReadOnly = True
        Me.txtDepCode.ReadOnly = True
        Me.txtReceiveCode.ReadOnly = True
        Me.txtPayCode.ReadOnly = True
        Me.txtGainCode.ReadOnly = True
        Me.txtDevCode.ReadOnly = True
        Me.txtStockCode.ReadOnly = True
        Me.txtIncAndDecCode.ReadOnly = True

        Me.txtWorkYear.ReadOnly = True



        '载入当前的账套参数信息
        LoadDefaultSetting()


    End Sub

    Private Sub LoadDefaultSetting()
        '载入当前的账套参数信息
        Try
            If sysCurrentLedgerSetting Is Nothing Then
                '未选择账套则退出
                Me.Close()
                Exit Sub
            End If

            If sysCurrentLedgerSetting.IndustryTypeId > 0 Then
                '已选择了科目，不用重新选择
                mIfSelectedIndustryAcct = True
            Else
                mIfSelectedIndustryAcct = False
            End If

            '将当前账套参数加载到窗体控件上
            With sysCurrentLedgerSetting
                '第一个 panel:基本账套信息
                '如果已经启用，则禁止选择科目按钮
                '判断是否已经启用，并输入凭证
                '如果已有凭证，则禁用行业科目按钮、禁用 本位币 控件
                Me.btnSelectIndustryAcct.Enabled = Not (.LedgerIfOpened)

                Me.txtCompanyName.Text = .CompanyName
                Me.txtIndustryType.Text = .IndustryType
                Me.txtOwnerCurrency.Text = .LedgerOwnerCurrency
                Me.txtOneAcctLength.Text = .OneAcctLevelLength.ToString
                Me.txtSubAcctLength.Text = .LedgerSubAcctLength.ToString

                Me.txtCashCode.Text = .CashCode
                Me.txtBankCode.Text = .BankCode
                Me.txtAssets.Text = .AssetsCode
                Me.txtDepCode.Text = .DepCode
                Me.txtReceiveCode.Text = .ReceiveCode
                Me.txtPayCode.Text = .PayCode
                Me.txtGainCode.Text = .GainCode
                Me.txtDevCode.Text = .DevCode
                Me.txtStockCode.Text = .StockCode
                Me.txtIncAndDecCode.Text = .IncAndDecCode

                '第二个 panel:打印设置
                Me.chkPrintQuantity.Checked = .IfPrintQuantity
                Me.chkPrintDate.Checked = .IfPrintDate '是否打印当前打印日期
                Me.chkPrintPerson.Checked = .IfPrintPerson
                Me.chkPrintManager.Checked = .IfPrintManager
                Me.chkPrintPoster.Checked = .IfPrintPoster
                Me.chkPrintNegative.Checked = .IfPrintNegative
                Me.chkPrintChecker.Checked = .IfPrintChecker
                Me.chkPrintBank.Checked = .IfPrintBankInfo
                Me.chkPrintMaker.Checked = .IfPrintMaker
                Me.chkPrintCasher.Checked = .IfPrintCasher
                Me.chkPrintForeign.Checked = .IfPrintForeignMoney
                Me.chkPrintQuantity.Checked = .IfPrintQuantity
                Me.chkSelfCheck.Checked = .IfSelfCheckVoucher
                Me.chkSelfPost.Checked = .IfSelfPostVoucher

                '第三个panel:启用
                Me.chkIfOpened.Checked = .LedgerIfOpened
                If .LedgerIfOpened Then
                    '已启用
                    'Me.txtOpenDate.Text = .LedgerOpenedDate
                    Me.dtPicker.Value = .LedgerOpenedDate
                    Me.txtOpenWorkYear.Text = .LedgerOpenWorkYear
                    Me.txtWorkYear.Text = .LedgerWorkYear

                    Me.chkIfOpened.Enabled = False '不能修改当前启用状态
                    Me.btnOpenLedger.Enabled = False '停用启用按钮
                    Me.dtPicker.Enabled = False '不能修改日期
                    Me.txtOpenWorkYear.Enabled = False
                    Me.txtWorkYear.Enabled = False
                Else
                    '未启用
                    Me.chkIfOpened.Enabled = True  '可修改当前启用状态
                    Me.btnOpenLedger.Enabled = True  '允许启用按钮
                    Me.dtPicker.Enabled = True  '不能修改日期
                    Me.txtOpenWorkYear.Enabled = True
                    Me.txtWorkYear.Enabled = True

                    Me.dtPicker.Value = Now().ToString("d")
                    'Me.txtOpenDate.Text = "" '当下日期，点击启用按钮后，这里填写当前日期，不能手工修改
                    Me.txtOpenWorkYear.Text = Now().Year.ToString() & Now().Month.ToString("D2") '"" '启用的会计期间
                    Me.txtWorkYear.Text = Now().Year.ToString() & Now().Month.ToString("D2") '"" '此处的值应等于启用的第一个会计期间
                End If

                '第四个panel：AB账
                '判断是否是注册用户，如果不是，则无法使用AB账功能
                '隐藏AB账设置，显示一条信息
                If Not sysClsApp.AppRegisted Then
                    Me.lblAbT1.Visible = False
                    Me.lblAbT2.Visible = False
                    Me.lblAbT3.Visible = False
                    Me.lblT4.Visible = False
                    Me.lblShowInfo.Visible = True
                Else
                    Me.lblShowInfo.Visible = False
                    Me.lblAbT1.Visible = True
                    Me.lblAbT2.Visible = True
                    Me.lblAbT3.Visible = True
                    Me.lblT4.Visible = True

                    '载入所有账套信息
                    mFrmLedgerList = GetAllLedgers()
                    '加载comboBox列表
                    Me.cmbLedgerAbSetup.Items.Clear()
                    Me.cmbLedgerAbSetup.Items.AddRange(sysAbState)
                    '如果当前账套已分AB设置，则选择
                    Me.cmbLedgerAbSetup.SelectedIndex = sysCurrentLedgerSetting.LedgerAbSetup

                    '加载comboBox relativeLedger列表
                    Me.cmbRelativeLedger.Items.Clear()
                    For Each tmpL As ClsLedger In mFrmLedgerList
                        If tmpL.LedgerId <> sysCurrentClsLedger.LedgerId Then
                            Me.cmbRelativeLedger.Items.Add(tmpL.LedgerName)
                        End If
                    Next
                    '如果当前账套已有选择，则选择
                    If sysCurrentLedgerSetting.RelativeLedgerId <> 0 Then
                        Me.cmbRelativeLedger.SelectedIndex = mFrmLedgerList.IndexOf(sysCurrentClsLedger) 'sysCurrentLedgerSetting.RelativeLedgerId
                    End If
                End If
            End With
        Catch ex As Exception
            mErrorCreater = "frmFinanicalSetupGlobalPrint - LoadDefaultSetting"
            mErrorNumber = -1003
            mErrorMessage = ex.Message
        End Try
    End Sub

    Private Function GetAllLedgers() As List(Of ClsLedger)
        Try
            '取出库
            Dim tmpLedgerDb As ClsLedgerDbAccess = New ClsLedgerDbAccess(sysAppInfoConnectionString)
            Return tmpLedgerDb.GetAllLedger()

            'Return mFrmLedgerList
        Catch ex As OleDb.OleDbException
            mErrorNumber = ex.ErrorCode
            mErrorMessage = ex.Message
            mErrorCreater = "frmFinacialSetupGlobalPrint - GetAllLedgers"
            Return Nothing
        End Try

    End Function

    Private Sub btnOpenLedger_Click(sender As Object, e As EventArgs) Handles btnOpenLedger.Click
        '启用按钮
        '检查是否选择了某行业的标准科目
        If Not mIfSelectedIndustryAcct Then
            MessageBox.Show("请先选择行业科目。", "账套启用提示")
            Exit Sub
        End If

        '选择了，则可以启用
        '判断启用日期是否非法（超过当前日期）
        If Me.dtPicker.Value.CompareTo(Now()) > 0 Then
            MessageBox.Show("启用日期不能晚于当前日期。", "启用账套")
            Return
        End If
        '判断是否填写了会计期间
        Dim tmpOpenWorkYear = Me.txtOpenWorkYear.Text.Trim
        If String.IsNullOrEmpty(tmpOpenWorkYear) Then
            MessageBox.Show("请填写启用会计期间，正确格式为：202102", "启用账套")
            Return
        End If

        '判断当前启用期间格式是否正确
        Dim tmpK As String = Me.txtOpenWorkYear.Text.Trim()
        If tmpK.Length <> 6 Then
            MessageBox.Show("请填写启用会计期间，正确格式为：202102", "启用账套")
            Return

        End If

        Dim tmpN As Int16 = tmpK.Substring(0, 4)
        Dim tmpY As Int16 = tmpK.Substring(4)
        Try
            Date.Parse(tmpN & "/" & tmpY)
        Catch ex As FormatException
            MessageBox.Show("请填写启用会计期间，正确格式为：202102", "启用账套")
            Return
        End Try

        '判断当前会计期间是否晚于当前日期
        If tmpN > Now().Year OrElse tmpY > Now().Month Then
            MessageBox.Show("账套启用期间年度与月份不能晚于当前年或月，正确格式为：202102", "启用账套")
            Return
        End If

        '将内容写入 参数类对象
        sysCurrentLedgerSetting.LedgerIfOpened = True
        sysCurrentLedgerSetting.LedgerOpenedDate = Me.dtPicker.Value
        sysCurrentLedgerSetting.LedgerOpenWorkYear = tmpOpenWorkYear
        sysCurrentLedgerSetting.LedgerWorkYear = tmpOpenWorkYear
        '修改控件状态
        Me.chkIfOpened.Checked = True
        '启用按钮禁用
        Me.dtPicker.Enabled = False
        Me.btnOpenLedger.Enabled = False
        Me.chkIfOpened.Enabled = False
        Me.txtOpenWorkYear.Enabled = False
        Me.txtWorkYear.Enabled = False
    End Sub

    Private Sub btnSelectIndustryAcct_Click(sender As Object, e As EventArgs) Handles btnSelectIndustryAcct.Click
        '选择行业科目
        Dim tmpForm As frmIndustryAcctSelect = New frmIndustryAcctSelect
        tmpForm.ShowDialog()
    End Sub

    Private Sub frmFinancialSetupGlobalPrint_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
        '移除订阅事件
        RemoveHandler ClsBroker.OnChange, AddressOf GetSelectedIndustryAcctType

        '移除自定义事件订阅
        RemoveHandler ClsBroker.OnCustomCtrolContentChange, AddressOf OnCustomControlTextChanged

        '保存当前修改
        If mIfSettingEdit Then
            ApplySetting()
        End If



    End Sub

    Private Sub WriteLedgerSetting()

        Try
            '保存对象到表中
            Dim tmpSettingDb As ClsTLedgerSettingDbAccess = New ClsTLedgerSettingDbAccess(sysUserDataConnectionString)
            If tmpSettingDb.UpdateTLedgerSetting(sysCurrentLedgerSetting) = 0 Then
                MessageBox.Show("账套参数保存失败。", "全局参数设置")
            Else
                'MessageBox.Show("账套参数保存成功。", "全局参数设置")
            End If
            '清空表内其他所有数据，为写入凭证做准备。


        Catch ex As OleDb.OleDbException
            mErrorCreater = "frmFinancialSetupGlobalPrint - Closing"
            mErrorMessage = ex.Message
            mErrorNumber = ex.ErrorCode
        End Try

    End Sub

    Private Sub btnRestore_Click(sender As Object, e As EventArgs) Handles btnRestore.Click
        '重新导入默认参数
        LoadDefaultSetting()
    End Sub

    Private Sub btnApply_Click(sender As Object, e As EventArgs) Handles btnApply.Click
        '应用当前填写内容到类对象
        ApplySetting()
    End Sub

    Private Sub ApplySetting()
        '把当前的控件内容写入类对象中并禁止 恢复默认按钮
        Try
            '如果没有选择或者内容没有改变,则退出
            If Not mIfSelectedIndustryAcct Or Not mIfSettingEdit Then
                Exit Sub
            End If

            '将窗体控件上内容加载到当前账套参数变量
            With sysCurrentLedgerSetting
                '第一个 panel:基本账套信息
                .CompanyName = Me.txtCompanyName.Text
                .IndustryType = Me.txtIndustryType.Text
                .LedgerOwnerCurrency = Me.txtOwnerCurrency.Text
                .OneAcctLevelLength = Me.txtOneAcctLength.Text
                .LedgerSubAcctLength = Me.txtSubAcctLength.Text

                .CashCode = Me.txtCashCode.Text
                .BankCode = Me.txtBankCode.Text
                .AssetsCode = Me.txtAssets.Text
                .DepCode = Me.txtDepCode.Text
                .ReceiveCode = Me.txtReceiveCode.Text
                .PayCode = Me.txtPayCode.Text
                .GainCode = Me.txtGainCode.Text
                .DevCode = Me.txtDevCode.Text
                .StockCode = Me.txtStockCode.Text
                .IncAndDecCode = Me.txtIncAndDecCode.Text

                '第二个 panel:打印设置
                .IfPrintQuantity = Me.chkPrintQuantity.Checked
                .IfPrintDate = Me.chkPrintDate.Checked
                .IfPrintPerson = Me.chkPrintPerson.Checked
                .IfPrintManager = Me.chkPrintManager.Checked
                .IfPrintPoster = Me.chkPrintPoster.Checked
                .IfPrintNegative = Me.chkPrintNegative.Checked
                .IfPrintChecker = Me.chkPrintChecker.Checked
                .IfPrintBankInfo = Me.chkPrintBank.Checked
                .IfPrintMaker = Me.chkPrintMaker.Checked
                .IfPrintCasher = Me.chkPrintCasher.Checked
                .IfPrintForeignMoney = Me.chkPrintForeign.Checked
                .IfSelfCheckVoucher = Me.chkSelfCheck.Checked
                .IfSelfPostVoucher = Me.chkSelfPost.Checked
                '.IfPrintQuantity = Me.chkPrintQuantity.Checked

                '第三个panel:启用
                .LedgerIfOpened = Me.chkIfOpened.Checked
                .LedgerOpenedDate = Me.dtPicker.Value
                .LedgerOpenWorkYear = Me.txtOpenWorkYear.Text
                .LedgerWorkYear = Me.txtWorkYear.Text


                '第四个panel：AB账
                '判断是否是注册用户，如果不是，则无法使用AB账功能
                .LedgerAbSetup = Me.cmbLedgerAbSetup.SelectedIndex
                .RelativeLedgerId = Me.cmbRelativeLedger.SelectedIndex


            End With
            '禁用默认设置功能
            Me.btnRestore.Enabled = False


            '写入库中,使用线程
            Dim t As Threading.Thread = New Threading.Thread(AddressOf WriteLedgerSetting)
            t.Start()

            mIfSettingEdit = False

        Catch ex As Exception
            mErrorCreater = "frmFinanicalSetupGlobalPrint - ApplySetting"
            mErrorNumber = -1004
            mErrorMessage = ex.Message
        End Try
    End Sub

    Private Sub txtCompanyName_TextChanged(sender As Object, e As EventArgs)
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub txtOwnerCurrency_TextChanged(sender As Object, e As EventArgs)
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub txtSubAcctLength_TextChanged(sender As Object, e As EventArgs)
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub cmbLedgerAbSetup_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbLedgerAbSetup.SelectedIndexChanged
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub cmbRelativeLedger_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbRelativeLedger.SelectedIndexChanged
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub chkPrintQuantity_CheckedChanged(sender As Object, e As EventArgs) Handles chkPrintQuantity.CheckedChanged
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub chkPrintForeign_CheckedChanged(sender As Object, e As EventArgs) Handles chkPrintForeign.CheckedChanged
        '标记内容修改
        mIfSettingEdit = True
    End Sub

    Private Sub dtPicker_ValueChanged(sender As Object, e As EventArgs) Handles dtPicker.ValueChanged
        Dim tmpDate As Date = Me.dtPicker.Value
        Me.txtOpenWorkYear.Text = tmpDate.Year.ToString & tmpDate.Month.ToString("D2")
    End Sub

    Private Sub txtOpenWorkYear_TextChanged(sender As Object, e As EventArgs) Handles txtOpenWorkYear.TextChanged
        Dim tmpK As String = Me.txtOpenWorkYear.Text.Trim()
        If tmpK.Length <> 6 Then
            Return
        End If
        Me.txtWorkYear.Text = tmpK

    End Sub
End Class